<template>
    <div class="login-container">
        <el-form ref="form" :model="form" :rules="rules" class="login-page">
            <h2 class="title" style="margin-bottom: 20px">用户注册</h2>
            <el-form-item prop="username">
                <el-input v-model="form.userName" placeholder="请输入用户名" clearable>
                    <template #prefix>
                        <el-icon class="el-input__icon">
                            <User />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="nickname">
                <el-input v-model="form.name" placeholder="请输入昵称" clearable>
                    <template #prefix>
                        <el-icon class="el-input__icon">
                            <User />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="email">
                <el-input v-model="form.email" placeholder="请输入邮箱" clearable>
                    <template #prefix>
                        <el-icon class="el-input__icon">
                            <Email />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="sex">
                <el-radio v-model="form.sex" label="1">男</el-radio>
                <el-radio v-model="form.sex" label="2">女</el-radio>
                <el-radio v-model="form.sex" label="3">其他 / 不透露</el-radio>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="form.password" placeholder="请输入密码" clearable show-password>
                    <template #prefix>
                        <el-icon class="el-input__icon">
                            <Lock />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="confirm">
                <el-input v-model="form.confirm" placeholder="请再次确认密码" clearable show-password>
                    <template #prefix>
                        <el-icon class="el-input__icon">
                            <Lock />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style=" width: 100%;font-size: 20px" @click="register">注 册</el-button>
            </el-form-item>
            <el-form-item><el-button type="text" style="font-size: 16px" @click="$router.push('/login')">前往登录>>
                </el-button></el-form-item>
        </el-form>
    </div>
</template>
    
<script>
import request from "../utils/request";
import { ElMessage } from "element-plus";
export default {
    name: "Login",
    data() {
        return {
            form: {},
            validCode: '',
            rules: {
                userName: [
                    {
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur',
                    },
                    {
                        min: 2,
                        max: 13,
                        message: '长度要求为2到13位',
                        trigger: 'blur',
                    },
                ],
                name: [
                    {
                        required: true,
                        message: '请输入昵称',
                        trigger: 'blur',
                    },
                    {
                        min: 1,
                        max: 13,
                        message: '长度要求为1到13位',
                        trigger: 'blur',
                    },
                ],
                email: [
                    {
                        required: true,
                        message: '请输入邮箱',
                        trigger: 'blur',
                    },
                    {
                        type: 'email',
                        message: '请输入正确的邮箱地址',
                        trigger: ['blur', 'change'],
                    },
                ],
                password: [
                    {
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur',
                    }
                ],
                confirm: [
                    {
                        required: true,
                        message: "请确认密码",
                        trigger: "blur"
                    }
                ],
            }
        }
    },

    methods: {
        createValidCode(data) {
            this.validCode = data
        },
        register() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.password != this.form.confirm) {
                        ElMessage.error("两次密码输入不一致")
                        return
                    }
                    delete this.form.confirm
                    request.post("/login/register", this.form).then(res => {
                        if (res.code == 1) {
                            ElMessage.success("注册成功")
                            this.$router.push("/login")
                        }
                        else { ElMessage.error("请求错误" + res.msg) }
                    })
                }
            })

        }
    }

}

</script>
    
<style scoped>
.login-container {
    position: fixed;
    width: 100%;
    height: 100%;
    background: url('../img/bg2.svg');
    background-size: contain;
}

.login-page {
    border-radius: 5px;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
}</style>